<?php

namespace App\Console\Commands;

use App\Library\Logic\ShopCashOutLogLogic;
use App\Library\Logic\WithdrawLogic;
use App\Models\ShopBankCardModel;
use Illuminate\Console\Command;

class SyncCashOutToWithdrawCommand extends Command
{
    /**
     * 同步shop_cash_out_log数据至withdraw
     *
     * @var string
     */
    protected $signature = 'command:cashOut';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * 同步shop_cash_out_log数据至withdraw
     * Execute the console command.
     */
    public function handle(): void
    {
        foreach (ShopCashOutLogLogic::all() as $value) {
            if ((int)$value->bankCard->bc_type === 1) $objectType = 2;
            else if ((int)$value->bankCard->bc_type === 2) $objectType = 1;
            else $objectType = 3;

            if ((int)$value->col_status === 1) $status = 0;
            else if ((int)$value->col_status === 2) $status = 2;
            else $status = 1;

            WithdrawLogic::insert([
                'serial_number' => serialNumber(),
                'user_id' => $value->user_id,
                'payment_method_id' => $objectType,
                'name' => $value->bankCard->bc_name,
                'identity' => $value->bankCard->bc_number,
                'bank_name' => $value->bankCard->bc_bank_name,
                'amount' => $value->col_price,
                'fee' => $value->col_handling_price,
                'status' => $status,
                'status_arrival' => (int)$value->col_success_time === 0 ? 0 : 1,
                'remark' => $value->col_remark,
                'created_at' => now()->timestamp($value->created_at)->format('Y-m-d H:i:s')
            ]);
        }
    }
}
